package com.example.mybutton;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "leo";
    private  EditText et;
    private ProgressBar progressBar,progressBar2;
    private NotificationManager manager;
    private Notification notification;

    private LinearLayout dialog_bg;
    private TextView dialog_bg_text;
    private RadioButton[] radioButtons=null;
    int index=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取按钮id
        Button btn=findViewById(R.id.btn);
        //获取进度条id
        progressBar =findViewById(R.id.pd);
        progressBar2 =findViewById(R.id.pd2);
        //不显示进度条View.GONE；默认显示View.VISIBLE
        progressBar.setVisibility(View.GONE);
        //获取通知管理器对象
        manager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            NotificationChannel channel= new NotificationChannel("leo","测试通知"
                    ,NotificationManager.IMPORTANCE_HIGH);//最后一个参数为通知的权限
            manager.createNotificationChannel(channel);
        }

        Intent intent=new Intent(this,NotificationActivity.class);  //创建跳转意图NotificationActivity.class
        PendingIntent pendingIntent= PendingIntent.getActivity(this,0,intent,0);
        notification= new NotificationCompat.Builder(this,"leo")
                .setContentTitle("官方通知")  //设置标题
                .setContentText("世界这么大我想去走走")  //设置文本内容
                .setSmallIcon(R.drawable.ic_baseline_person_24)  //设置小图标（不带颜色）
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.test_2))  //设置大图标
                .setColor(Color.parseColor("#ff0000"))  //设置小图标颜色
                .setContentIntent(pendingIntent)  //设置点击通知跳转意图
                .setAutoCancel(true)  //设置点击通知自动清楚
                .setWhen(System.currentTimeMillis())  //设置通知被创建的时间
                .build();
        //点击事件
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e(TAG,"onClick:");

            }
        });
        //长按事件
        btn.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                Log.e(TAG,"onLongClick:");
                return false;
            }
        });
        //触摸事件
        btn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.e(TAG,"OnTouch:"+event.getAction());
                return true;
            }
        });


        //点击获取用户账号
        Button gitedit1=findViewById(R.id.gitedit1);
        et=findViewById(R.id.edit1);
        gitedit1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //取消通知
                manager.cancel(1);
                //获取用户信息转为字符串
                String text =et.getText().toString();
                Log.e(TAG,"输入的内容"+text);
                if (progressBar.getVisibility()==View.GONE){
                    progressBar.setVisibility(View.VISIBLE);
                }else {
                    progressBar.setVisibility(View.GONE);
                }
            }
        });

    }
//自定义点击事件
    public void leoclick(View view) {
        Log.e(TAG,"onleoClick:");
        Button bt1=findViewById(R.id.btn);


    }

    public void Load(View view) {
        //发送通知
        manager.notify(1,notification);
        //进度条每次加10%
        int progress=progressBar2.getProgress();
        progress+=10;
        progressBar2.setProgress(progress);

    }
    public void sendNotification(View view){
        manager.notify(1,notification);
    }
    public void cacelNotification(View view){manager.cancel(1);

    }
    private void function(){
        RadioGroup radiogroup = findViewById(R.id.radioGroup);

        radioButtons= new RadioButton[]{(RadioButton)findViewById(R.id.r0),
                (RadioButton)findViewById(R.id.r1),
                (RadioButton) findViewById(R.id.r2),
                };
        switch (index){
            case 0:
                radiogroup.check(R.id.r0);
                break;
            case 1:
                radiogroup.check(R.id.r1);
                break;
            case 2:
                radiogroup.check(R.id.r2);
                break;


        }
        radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.r0:
                        index=0;
                        Log.e("TAG","checkedId0");
                        break;
                    case R.id.r1:
                        index=1;
                        Log.e("TAG","checkedId1");
                        break;
                    case R.id.r2:
                        index=2;
                        Log.e("TAG","checkedId2");
                        break;

                }


            }
        });
    }


}